include(GetPlatformInfo)

if(NOT OS_IS_WIN)
    return()
endif()

include(InstallRequiredSystemLibraries)

set(CPACK_PACKAGE_NAME ${MUSE_APP_NAME})
set(CPACK_PACKAGE_DESCRIPTION_SUMMARY "Audacity is a digital audio editor")
set(CPACK_PACKAGE_VENDOR "Audacity")
set(CPACK_PACKAGE_CONTACT "https://audacityteam.org")
set(CPACK_PACKAGE_HOMEPAGE_URL "https://audacityteam.org")

set(CPACK_PACKAGE_VERSION_MAJOR "${MUSE_APP_VERSION_MAJOR}")
set(CPACK_PACKAGE_VERSION_MINOR "${MUSE_APP_VERSION_MINOR}")
set(CPACK_PACKAGE_VERSION_PATCH "${MUSE_APP_VERSION_PATCH}")
set(CPACK_PACKAGE_VERSION_BUILD "${CMAKE_BUILD_NUMBER}")
set(CPACK_PACKAGE_VERSION "${MUSE_APP_VERSION_MAJOR}.${MUSE_APP_VERSION_MINOR}.${MUSE_APP_VERSION_PATCH}.${CPACK_PACKAGE_VERSION_BUILD}")
message("CPACK_PACKAGE_VERSION: ${CPACK_PACKAGE_VERSION}")

set(git_date_string "")

if(MUSE_APP_UNSTABLE)
    find_program(GIT_EXECUTABLE git PATHS ENV PATH)

    if(GIT_EXECUTABLE)
        execute_process(
            COMMAND "${GIT_EXECUTABLE}" log -1 --date=short --format=%cd
            WORKING_DIRECTORY "${PROJECT_SOURCE_DIR}"
            OUTPUT_VARIABLE git_date
            OUTPUT_STRIP_TRAILING_WHITESPACE)
    endif()

    if(git_date)
        string(REGEX REPLACE "-" "" git_date "${git_date}")
        set(git_date_string "~git${git_date}")
    endif()
endif(MUSE_APP_UNSTABLE)

set(CPACK_PACKAGE_FILE_NAME "${MUSE_APP_NAME}-${MUSE_APP_VERSION}${git_date_string}")
set(CPACK_PACKAGE_INSTALL_DIRECTORY ${MUSE_APP_NAME_VERSION})

set(MUSE_EXECUTABLE_NAME ${MUSE_APP_NAME}${MUSE_APP_VERSION_MAJOR})
set(CPACK_PACKAGE_EXECUTABLES "${MUSE_EXECUTABLE_NAME}" "${MUSE_APP_TITLE_VERSION}") # exe name, label
set(CPACK_CREATE_DESKTOP_LINKS "${MUSE_EXECUTABLE_NAME}" "${MUSE_APP_TITLE_VERSION}") # exe name, label

# Wix-specific options
set(CPACK_GENERATOR "WIX")

file(TO_CMAKE_PATH $ENV{PROGRAMFILES} PROGRAMFILES)
set(CPACK_WIX_ROOT "${PROGRAMFILES}/WiX Toolset v3.11")

# Use custom version of WIX.template.in
set(CMAKE_MODULE_PATH "${CMAKE_CURRENT_SOURCE_DIR}/buildscripts/packaging/Windows/Installer" ${CMAKE_MODULE_PATH})

if(NOT CPACK_WIX_PRODUCT_GUID)
    set(CPACK_WIX_PRODUCT_GUID "00000000-0000-0000-0000-000000000000")
endif()

message(STATUS "[SetupWindowsPackaging.cmake] CPACK_WIX_PRODUCT_GUID: ${CPACK_WIX_PRODUCT_GUID}")

if(NOT CPACK_WIX_UPGRADE_GUID)
    set(CPACK_WIX_UPGRADE_GUID "11111111-1111-1111-1111-111111111111")
endif()

message(STATUS "[SetupWindowsPackaging.cmake] CPACK_WIX_UPGRADE_GUID: ${CPACK_WIX_UPGRADE_GUID}")

set(CPACK_WIX_LICENSE_RTF "${PROJECT_SOURCE_DIR}/buildscripts/packaging/Windows/Installer/LICENSE.rtf")
set(CPACK_WIX_PRODUCT_ICON "${PROJECT_SOURCE_DIR}/share/icons/AppIcon/AU4_AppIcon.ico")
set(CPACK_WIX_UI_BANNER "${PROJECT_SOURCE_DIR}/buildscripts/packaging/Windows/Installer/installer_banner_wix.png")
set(CPACK_WIX_UI_DIALOG "${PROJECT_SOURCE_DIR}/buildscripts/packaging/Windows/Installer/installer_background_wix.png")
set(CPACK_WIX_PROGRAM_MENU_FOLDER "${MUSE_APP_TITLE_VERSION}")
set(CPACK_WIX_EXTENSIONS "WixUtilExtension")

# Extra CPack variables
list(APPEND CPACK_WIX_CANDLE_EXTRA_FLAGS
    "-dMUSE_APP_TITLE_VERSION=${MUSE_APP_TITLE_VERSION}"
    "-dMUSE_APP_TITLE=${MUSE_APP_TITLE}"
    "-dMUSE_EXECUTABLE_NAME=${MUSE_EXECUTABLE_NAME}"
    "-dMUSE_APP_RELEASE_CHANNEL=${MUSE_APP_RELEASE_CHANNEL}"
    "-dCPACK_PACKAGE_VERSION_MAJOR=${CPACK_PACKAGE_VERSION_MAJOR}"
)

if (MUSE_APP_IS_PRERELEASE)
    list(APPEND CPACK_WIX_CANDLE_EXTRA_FLAGS "-dMUSE_APP_IS_PRERELEASE=ON")
endif()

message(STATUS "========== Audacity Packaging Variables ==========")
  message(STATUS "MUSE_APP_TITLE='${MUSE_APP_TITLE}'")
  message(STATUS "MUSE_APP_TITLE_VERSION='${MUSE_APP_TITLE_VERSION}'")
  message(STATUS "MUSE_APP_RELEASE_CHANNEL='${MUSE_APP_RELEASE_CHANNEL}'")
  message(STATUS "MUSE_EXECUTABLE_NAME='${MUSE_EXECUTABLE_NAME}'")
  message(STATUS "CPACK_PACKAGE_NAME='${CPACK_PACKAGE_NAME}'")
  message(STATUS "CPACK_PACKAGE_INSTALL_DIRECTORY='${CPACK_PACKAGE_INSTALL_DIRECTORY}'")
  message(STATUS "CPACK_PACKAGE_VERSION_MAJOR='${CPACK_PACKAGE_VERSION_MAJOR}'")
  message(STATUS "CPACK_PACKAGE_VERSION_MINOR='${CPACK_PACKAGE_VERSION_MINOR}'")
  message(STATUS "CPACK_PACKAGE_VERSION_PATCH='${CPACK_PACKAGE_VERSION_PATCH}'")
  message(STATUS "CPACK_PACKAGE_EXECUTABLES='${CPACK_PACKAGE_EXECUTABLES}'")
  message(STATUS "CPACK_CREATE_DESKTOP_LINKS='${CPACK_CREATE_DESKTOP_LINKS}'")
  message(STATUS "CPACK_WIX_PROGRAM_MENU_FOLDER='${CPACK_WIX_PROGRAM_MENU_FOLDER}'")
  message(STATUS "CPACK_WIX_PRODUCT_ICON='${CPACK_WIX_PRODUCT_ICON}'")
  message(STATUS "CPACK_WIX_UI_BANNER='${CPACK_WIX_UI_BANNER}'")
  message(STATUS "CPACK_WIX_UI_DIALOG='${CPACK_WIX_UI_DIALOG}'")
  message(STATUS "CPACK_WIX_TEMPLATE='${CPACK_WIX_TEMPLATE}'")
  message(STATUS "CPACK_WIX_EXTENSIONS='${CPACK_WIX_EXTENSIONS}'")
  message(STATUS "CPACK_WIX_CANDLE_EXTRA_FLAGS='${CPACK_WIX_CANDLE_EXTRA_FLAGS}'")
  message(STATUS "==================================================")


include(CPack)
